package com.tsingteng.cosfun.ui.message;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.content.pm.PackageManager;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.KeyEvent;
import android.widget.FrameLayout;

import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.base.BaseActivity;
import com.tsingteng.cosfun.bean.DBBean;
import com.tsingteng.cosfun.bean.DBPlayBean;
import com.tsingteng.cosfun.callback.PermissionInterface;
import com.tsingteng.cosfun.ui.costar.VideoLocalFragment;
import com.tsingteng.cosfun.ui.login.LogonAgreementFragment;
import com.tsingteng.cosfun.ui.login.SplashFragment;
import com.tsingteng.cosfun.ui.message.addplay.WritingPlayFragment;
import com.tsingteng.cosfun.ui.message.chat.ChatFragment;
import com.tsingteng.cosfun.ui.message.comment.OtherFragment;
import com.tsingteng.cosfun.ui.message.comment.usercomment.UserCommentFragment;
import com.tsingteng.cosfun.ui.message.comment.videocomment.VideoCommentFragment;
import com.tsingteng.cosfun.ui.message.fans.FansFragment;
import com.tsingteng.cosfun.ui.message.geneal.BackListener;
import com.tsingteng.cosfun.ui.message.geneal.PlayDetailFragment;
import com.tsingteng.cosfun.ui.message.geneal.PlayFragment;
import com.tsingteng.cosfun.ui.message.messagereport.MessageReportFragment;
import com.tsingteng.cosfun.ui.message.officalnotification.OfficalNotificationFragment;
import com.tsingteng.cosfun.ui.message.systemnotification.SystemNotificationFragment;
import com.tsingteng.cosfun.ui.message.uploadreportfragment.UploadReportFragment;
import com.tsingteng.cosfun.ui.setting.MobileBindFragment;
import com.tsingteng.cosfun.utils.PermissionHelper;
import com.tsingteng.cosfun.utils.VideoUtils;
import com.umeng.socialize.UMShareAPI;

import butterknife.BindView;


/**
 * 描述 : 通用activity
 * 创建人: 肖利.
 * 时间:2018/8/6
 * qq(872179966)
 */

public class CommonActivity extends BaseActivity {
    public static Activity instance = null;
    public static final String FRAGMENT_KEY = "fragmnt_key";
    public static final int KEY_SPLASH = -1;
    public static final int KEY_FANS = 0;
    public static final int KEY_GREAT = 1;
    public static final int KEY_COMMENT = 2;
    public static final int KEY_AT_ME = 3;
    public static final int KEY_PLAY = 4;
    public static final int KEY_LOGON = 5;
    public static final int KEY_PLAY_DETAILS = 6;
    public static final int KEY_ADD_PLAY = 7;
    public static final int KEY_JUMP_SYSTEM = 8;
    public static final int KEY_JUMP_OFFICAL = 9;
    public static final int KEY_CHAT = 10;
    public static final int KEY_CHATREPORT = 11;
    public static final int KEY_FRAGMENTREPORT = 12;
    public static final int KEY_USERCOMMENT = 13;
    public static final int KEY_VIDEOCOMMENT = 15;
    public static final int KEY_MOBILE_BIND = 16;
    public static final int KEY_REPORTLIST = 17;
    public static final int KEY_AGREEMENT = 18;
    public static final int KEY_LOCAL_VIDEO = 19;

    @BindView(R.id.fragmentContent)
    FrameLayout fragmentContent;
    private int playId;//段子id
    private String playName;//段子id
    private String mobile;
    private DBPlayBean dbBean;
    private int whyReportId;
    private String whyReportMessage;
    private int type;
    private int workId;
    private String funType;
    private BackListener mBackListener;
    private boolean interception = false;
    private int agementType;
    private int fragmentKey;


    @Override
    protected void initView() {
        instance = this;
        swichFragment(getIntent());

    }

    /**
     * 切换fragment
     *
     * @param intent
     */
    public void swichFragment(Intent intent) {
//        replaceFragment(LoginActivity.newInstance());

        int type = intent.getIntExtra("type", 0);
        int workId = intent.getIntExtra("workId", 0);
        switch (fragmentKey) {
            case KEY_SPLASH://启动页面
                replaceFragment(SplashFragment.newInstance());
                break;
            case KEY_LOGON://登录页面
//                replaceFragment(LoginActivity.newInstance());
                break;
            case KEY_FANS://粉丝
                replaceFragment(FansFragment.newInstance());
                break;
            case KEY_GREAT://点赞
                replaceFragment(OtherFragment.newInstance(funType));
                break;
            case KEY_COMMENT://评论
                replaceFragment(OtherFragment.newInstance(funType));
                break;
            case KEY_AT_ME://@我
                replaceFragment(OtherFragment.newInstance(funType));
                break;
            case KEY_PLAY://便当
                replaceFragment(PlayFragment.newInstance());
                break;
            case KEY_PLAY_DETAILS://便当详情
                replaceFragment(PlayDetailFragment.newInstance(playId, playName));
                break;
            case KEY_ADD_PLAY://添加便当
                replaceFragment(WritingPlayFragment.newInstance(dbBean));
                break;
            case KEY_JUMP_SYSTEM://系统通知
                replaceFragment(SystemNotificationFragment.newInstance());
                break;
            case KEY_JUMP_OFFICAL://官方助手
                replaceFragment(OfficalNotificationFragment.newInstance());
                break;
            case KEY_CHAT://私信通知
                replaceFragment(ChatFragment.newInstance());
                break;
            case KEY_CHATREPORT://私信通知
                replaceFragment(MessageReportFragment.newInstance(type, workId));
                break;
            case KEY_FRAGMENTREPORT://举报具体信息及上传举报到后台
                replaceFragment(UploadReportFragment.newInstance(whyReportId,whyReportMessage,type,workId));
                break;
            case KEY_USERCOMMENT://点击@我，赞我，评论我等页面跳入评论列表页
//                replaceFragment(UserCommentFragment.newInstance(id));
                break;
            case KEY_VIDEOCOMMENT://点击@我，赞我，评论我等页面跳入视频列表页
//                replaceFragment(VideoCommentFragment.newInstance(id));
                break;
            case KEY_MOBILE_BIND://绑定手机号
                replaceFragment(MobileBindFragment.newInstance(mobile));
                break;
            case KEY_REPORTLIST://举报列表
                replaceFragment(MessageReportFragment.newInstance(type, workId));
                break;
            case KEY_AGREEMENT://用户协议
                replaceFragment(LogonAgreementFragment.newInstance(agementType));
                break;

            case KEY_LOCAL_VIDEO://本地视屏的选择
                replaceFragment(VideoLocalFragment.newInstance());
                break;

        }
    }

    public void replaceFragment(Fragment fragmnet) {
        replaceFragment(R.id.fragmentContent, fragmnet);
    }

    public void replaceFragment(@IdRes int id, Fragment fragment) {

        getSupportFragmentManager().beginTransaction().replace(id, fragment).commit();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_common;
    }

    @Override
    protected void getIntentData(Intent intent) {
        fragmentKey = intent.getIntExtra(FRAGMENT_KEY, -1);
        if (fragmentKey == KEY_CHAT) {
            isUpKeyBord =true;//私信的时候布局整体上移
        }
        playId = intent.getIntExtra("playId", -1);
        playName = intent.getStringExtra("playName");
        mobile = intent.getStringExtra("mobile");
        dbBean = (DBPlayBean) intent.getSerializableExtra("dbBean");
        //选择的举报类型的id，由举报列表返回
        whyReportId = intent.getIntExtra("whyReportId", -1);
        //选择的举报类型的message，由举报列表返回
        whyReportMessage = intent.getStringExtra("whyReportMessage");
        //举报类型 1视频举报 2用户举报 3评论举报 4私信举报 5便当举报 6尬评举报
        type = intent.getIntExtra("type", -1);
        //	举报id 如果是作品举报，那么就是videoid，如果是用户举报就是userid，如果是评论举报就是commentid，私信也是私信的相应id
        workId = intent.getIntExtra("workId", -1);
        //点击@我，赞我，评论我等页面跳入评论列表页
        funType = intent.getStringExtra("funType");
        agementType = intent.getIntExtra("agementType", 1);//用户协议

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
    public void setBackListener(BackListener listener){
        this.mBackListener=listener;
    }
    public void setInterception(boolean minterception){
        this.interception=minterception;
    }
    public boolean isInterception(){
        return interception;
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (isInterception()) {
                if (mBackListener != null) {
                    mBackListener.backListener();
                    return false;
                }
            }
        }
        return super.onKeyDown(keyCode, event);

    }
}
